﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FacialClub.Models;
using FacialClub.Helpers;
using FacialClub.Models.Entities;
using FacialClub.Models.Interfaces;
using FacialClub.Models.Repositories;
using FacialClub.Models.ViewModels;

namespace FacialClub.Areas.Admin.Controllers
{
    /// <summary>
    /// Email Template Controller
    /// </summary>
    public class EmailTemplateController : Controller
    {
        private const int DEFAULTPAGESIZE = 5;
        readonly FacialClubEntities _db = new FacialClubEntities();
        readonly IEmailTemplateRepository _emailTemplateRepository;
        public EmailTemplateController(): this(new EmailTemplateRepository()){}

        /// <summary>
        /// Email Template Controller
        /// </summary>
        /// <param name="repository"></param>
        public EmailTemplateController(IEmailTemplateRepository repository)
        {
            _emailTemplateRepository = repository;
        }
        //
        // GET: /EmailTemplate/
        //      /EmailTemplate/Page/2
        //      /EmailTemplate?q=term
         [Authorize]
        public ActionResult Index(string q, int? page)
        {
            IQueryable<EmailTemplate> emailTemplates = null;
            emailTemplates = !string.IsNullOrWhiteSpace(q) ? _emailTemplateRepository.FindEmailTemplateByName(q).OrderBy(d => d.CreatedOn) : _emailTemplateRepository.FindAllEmailTemplate().OrderBy(d => d.CreatedOn);
            var paginatedEmailTemplate = emailTemplates.ToPagedList(page.HasValue ? page.Value - 1 : 0, DEFAULTPAGESIZE);
            return View(paginatedEmailTemplate);
        }
        //
        // GET: /Admin/EmailTemplate/Details/5

        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new FileNotFoundResult { Message = "No email template found due to invalid email template id" };
            }

            EmailTemplate emailTemplate = _emailTemplateRepository.GetEmailTemplate(id.Value);

            if (emailTemplate == null)
            {
                return new FileNotFoundResult { Message = "No Email Template found for that id" };
            }

            return View(emailTemplate);
        }

        //
        // GET: /EmailTemplate/Create

        [Authorize]
        public ActionResult Create()
        {
            EmailTemplate emailTemplate = new EmailTemplate();
            return View(emailTemplate);
        }

        //
        // POST: /EmailTemplate/Create

        [Authorize]
        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult Create(EmailTemplate emailTemplate)
        {
            if (ModelState.IsValid)
            {
                emailTemplate.EmailTemplateName = emailTemplate.EmailTemplateName;
                emailTemplate.EmailTemplateDescription = HttpUtility.HtmlEncode(emailTemplate.EmailTemplateDescription);
                emailTemplate.IsVisible = emailTemplate.IsVisible;
                emailTemplate.CreatedOn = emailTemplate.CreatedOn;
                _emailTemplateRepository.Add(emailTemplate);
                _emailTemplateRepository.Save();

                return RedirectToAction("Details", new { id = emailTemplate.EmailTemplateId });
            }

            return View(emailTemplate);
        }
        
        //
        // GET: /Admin/EmailTemplate/Edit/5
        [Authorize]
        public ActionResult Edit(int? id)
        {
            var emailTemplate = _emailTemplateRepository.GetEmailTemplate(Convert.ToInt32(id));
            return View(emailTemplate);
        }

        //
        // POST: /Admin/EmailTemplate/Edit/5

        [Authorize]
        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult Edit(int id, FormCollection collection)
        {
            var emailTemplate = _emailTemplateRepository.GetEmailTemplate(id);

            try
            {
                //Save EmailTemplate
                //_emailTemplateRepository.Add(emailTemplate);
                //_emailTemplateRepository.Save();
                UpdateModel(emailTemplate, collection.ToValueProvider());
                _emailTemplateRepository.Save();
                return RedirectToAction("Index");
            }
            catch
            {
                return View(emailTemplate);
            }
        }

        //
        // HTTP GET: /EmailTemplate/Delete/1

        [Authorize]
        public ActionResult Delete(int id)
        {
            EmailTemplate emailTemplate = _emailTemplateRepository.GetEmailTemplate(id);

            if (emailTemplate == null)
                return View("NotFound");
            // Show Index view
            return RedirectToAction("Index");
        }

        //
        // POST: /Admin/EmailTemplate/Delete/5

        [HttpPost, Authorize]
        public ActionResult Delete(int id, FormCollection collection)
        {
            EmailTemplate emailTemplate = _emailTemplateRepository.GetEmailTemplate(id);

            if (emailTemplate == null)
                return View("NotFound");

            _emailTemplateRepository.Delete(emailTemplate);
            _emailTemplateRepository.Save();

            return View("Deleted");
        }

        protected override void HandleUnknownAction(string actionName)
        {
            throw new HttpException(404, "Action not found");
        }
    }
}
